package com.meituan.verintreport.task;

import com.meituan.verintreport.db.dao.AssesmentsOnFormMapper;
import com.meituan.verintreport.db.dao.EvalsOnFormMapper;
import com.meituan.verintreport.db.dto.AssesmentsOnFormDto;
import com.meituan.verintreport.db.dto.EvalsOnFormDto;
import com.meituan.verintreport.db.verint.VerintBaseTableDao;
import com.meituan.verintreport.util.DateUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

@Slf4j
@Data
@Component
@ConfigurationProperties(prefix = "base-table.evals-on-form")
public class BaseTableTaskEvalsOnForm extends BaseTableTask {
    @Resource
    private EvalsOnFormMapper evalsOnFormMapper;

    @Resource
    private VerintBaseTableDao verintBaseTableDao;

    @Value("${report.timezone}")
    private String reportTimeZone;

    @Value("${base-table.evals-on-form.maxMinutesPerTime}")
    private int maxMinutesPerTime = 15;

    @Scheduled(cron = "${base-table.evals-on-form.cron}")
    public void execute() {
        if (!isEnable()) {
            return;
        }

        // long startTime = System.currentTimeMillis();
        // TimeZone timezone = TimeZone.getTimeZone(reportTimeZone);
        // Calendar beforeTime = Calendar.getInstance();
        // beforeTime.setTimeZone(timezone);
        // beforeTime.add(Calendar.HOUR_OF_DAY, -2);// 1小时// 之前的时间
        // beforeTime.set(Calendar.MINUTE, 0);
        // beforeTime.set(Calendar.SECOND, 0);
        // beforeTime.set(Calendar.MILLISECOND, 0);
        // Date beginTime = beforeTime.getTime();
        //
        // Calendar beforeTime1 = Calendar.getInstance();
        // beforeTime1.setTimeZone(timezone);
        // beforeTime1.add(Calendar.HOUR_OF_DAY, -1);
        // beforeTime1.set(Calendar.MINUTE, 0);
        // beforeTime1.set(Calendar.SECOND, 0);
        // beforeTime1.set(Calendar.MILLISECOND, 0);
        // Date endDateTime = beforeTime1.getTime();
        //
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // sdf.setTimeZone(timezone);
        //
        // String startTimeStr = sdf.format(beginTime) + ".000";
        // String endTimeStr = sdf.format(endDateTime) + ".000";
        // log.info("timezone = {} startTime = {}  endTime = {}", timezone.getDisplayName(), startTimeStr, endTimeStr);

        int pageIndex = 0;
        int pageSize = 1000;
        String start = DateUtil.getDate(21, null);
        String end = DateUtil.getDate(2,null);
        log.info("remove start:{},end:{}", start, end);
        Integer rs = evalsOnFormMapper.removeByTime(start , end);
        log.info("remove {} data ", rs);

        boolean shouldContinue;
        do {
            shouldContinue = false;
            String start2 = DateUtil.getDate(21, null);
            String end2 = DateUtil.getDate(1,null);
            log.info("got start:{},end:{}", start2, end2);
            List<EvalsOnFormDto> records = verintBaseTableDao.getEvalsOnFormListBeforeDateTimePaged
                    (start2, end2, pageIndex, pageSize);
            log.info("got {} records", records.size());
            if (records.size() == pageSize) {
                shouldContinue = true;
            }

            for (EvalsOnFormDto record : records) {
                try {
                    evalsOnFormMapper.insert(record);
                } catch (Exception ex) {
                    log.error("Add record to db exception.", ex);
                }
            }


//            long endTime = System.currentTimeMillis();
//            if (endTime - startTime > maxMinutesPerTime * 60 * 1000) {
//                shouldContinue = false;
//            }
            pageIndex += pageSize;
        }
        while (shouldContinue);
    }
}
